package com.cookor.poi.modules.phy.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.modules.phy.entity.TStock;
import com.cookor.poi.modules.phy.schedule.YurenSchedule;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping({"/data"})
public class YuRenController {

    @RequestMapping({"index"})
    public String dataIndex(HttpServletRequest request, HttpServletResponse response, Model model) {
        try {
            JSONObject data = YurenSchedule.getData();

            JSONArray array = data.getJSONObject("body").getJSONArray("rows");
            JSONObject bean;
            int age_0_18 = 0;
            int age_18_40 = 0;
            int age_40_65 = 0;
            int age_65_100 = 0;

            int sex_1 = 0;
            int sex_2 = 0;

            int bmi_18_5 = 0;
            int bmi_24 = 0;
            int bmi_28 = 0;
            int bmi_up = 0;

            int status_ing = 0;
            int status_ok = 0;

            int date_1 = 0;
            int date_2 = 0;
            int date_3 = 0;
            int date_4 = 0;
            int date_5 = 0;
            int date_6 = 0;
            int date_7 = 0;

            Integer age;
            Integer sex;
            double bmi;
            long date;

            Map<String, Object> result = new HashMap<>();
            // age bmi sex date state
            for(int i = 0; i < array.size() -1 ; i++){
                bean = array.getJSONObject(i).getJSONObject("patientModel");
                age = getAge(bean.getString("birth"));
                if(age <=18 && age > 0){
                    age_0_18++;
                } else if ( age > 18 && age <= 40) {
                    age_18_40++;
                } else if (age > 40 && age <= 65) {
                    age_40_65++;
                } else if( age > 65){
                    age_65_100++;
                }
                result.put("age_0_18", age_0_18);
                result.put("age_18_40", age_18_40);
                result.put("age_40_65", age_40_65);
                result.put("age_65_100", age_65_100);

                sex = bean.getInteger("sex");
                if(sex == 1){
                    sex_1++;
                } else if (sex == 2) {
                    sex_2++;
                }
                result.put("sex_1", sex_1);
                result.put("sex_2", sex_2);

                if(StringUtils.isNotBlank(bean.getString("height")) && StringUtils.isNotBlank(bean.getString("weight"))){
                    bmi = calculateBMI(bean.getDoubleValue("height"), bean.getDoubleValue("weight"));
                    if(bmi < 18.5){
                        bmi_18_5++;
                    } else if (bmi >= 18.5 && bmi_18_5 < 24) {
                        bmi_24++;
                    } else if (bmi >= 24 && bmi < 28) {
                        bmi_28++;
                    } else if (bmi >= 28) {
                        bmi_up++;
                    }
                }
                result.put("bmi_18_5", bmi_18_5);
                result.put("bmi_24", bmi_24);
                result.put("bmi_28", bmi_28);
                result.put("bmi_up", bmi_up);

                if(array.getJSONObject(i).getInteger("reportFlag") == 0){
                    status_ing++;
                }else{
                    status_ok++;
                }

                result.put("status_ing", status_ing);
                result.put("status_ok", status_ok);

                date = calculateDaysDifference(LocalDate.now(), LocalDate.parse(array.getJSONObject(i).getString("orderDate"),
                        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                if(date >=0 && date <1){
                    date_1++;
                } else if (date >= 1 && date < 2) {
                    date_2++;
                } else if (date >= 2 && date < 3) {
                    date_3++;
                } else if (date >= 3 && date < 4) {
                    date_4++;
                } else if (date >= 4 && date < 5) {
                    date_5++;
                } else if (date >= 5 && date < 6) {
                    date_6++;
                } else if (date >= 6 && date < 7) {
                    date_7++;
                }
                result.put("date_1", date_1);
                result.put("date_2", date_2);
                result.put("date_3", date_3);
                result.put("date_4", date_4);
                result.put("date_5", date_5);
                result.put("date_6", date_6);
                result.put("date_7", date_7);
            }
            model.addAttribute("result", JSONObject.toJSONString(result));
        }catch (Exception e){
            e.printStackTrace();
            return "error/400";
        }
        return "modules/yuren/data";
    }


    public static Integer getAge(String birthDateString){
        // 将字符串解析为LocalDate对象
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate birthDate = LocalDate.parse(birthDateString, formatter);
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 计算年龄
        Period age = Period.between(birthDate, currentDate);
        // 输出年龄
        return age.getYears();
    }

    // 计算BMI的方法
    private static double calculateBMI(double height, double weight) {
        return weight / (height / 100.0 * height / 100.0);
    }

    // 计算日期差距的方法
    private static long calculateDaysDifference(LocalDate currentDate, LocalDate targetDate) {
        return Math.abs(currentDate.toEpochDay() - targetDate.toEpochDay());
    }

}
